//
//  BrandDetailViewController.m
//  Mahjong
//
//  Created by mc on 16/9/18.
//  Copyright © 2016年 Elliot. All rights reserved.
//

#import "BrandDetailViewController.h"
#import "ClassifyCollectionViewCell.h"


#define WIDTH ([UIScreen mainScreen].bounds.size.width)
#define HEIGHT ([UIScreen mainScreen].bounds.size.height)
@interface BrandDetailViewController ()<UICollectionViewDataSource,UICollectionViewDelegate>
{
    UIView *leftbackView;
    NSArray *buttonArr;
    NSInteger btnTag;
}
@property (nonatomic, strong) UIView *backView;
@property (nonatomic, strong) UIButton *deleteButton;
@property (nonatomic, strong) UICollectionView *myCollectionVIew;


@end

@implementation BrandDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithWhite:0.900 alpha:1.000];

    buttonArr = [[NSArray alloc] initWithObjects:@"综合",@"销量",@"价格",@"推广", nil];
    self.navigationItem.title = @"品牌详情";
    
    [self createNaviBarItems];
    [self addOtherUI];
    [self addNearlySelectTableviewUI];
    [self addOrderStatesUI];
}
//// 处理导航栏和底部删除按钮
//- (UIStatusBarStyle)preferredStatusBarStyle
//{
//    return UIStatusBarStyleLightContent;
//}
- (void)createNaviBarItems{
    UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"nav_but_back"] style:(UIBarButtonItemStyleDone) target:self action:@selector(backAction)];
    backItem.tintColor = [UIColor grayColor];
    self.navigationItem.leftBarButtonItem = backItem;
    
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}

//-(void)addNav{
//    self.backView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, WIDTH, 40)];
//    _backView.backgroundColor = [UIColor whiteColor];
//    [self.view addSubview:_backView];
//    
//    UIButton *backBtn = [UIButton buttonWithType:UIButtonTypeCustom];
//    [backBtn setImage:[UIImage imageNamed:@"nav_but_back"] forState:UIControlStateNormal];
//    backBtn.backgroundColor = [UIColor clearColor];
//    backBtn.titleLabel.font = [UIFont systemFontOfSize:14];
//    [backBtn addTarget:self action:@selector(backBtnAction) forControlEvents:UIControlEventTouchUpInside];
//    backBtn.frame = CGRectMake(20, 5, 25, 25);
//    [_backView addSubview:backBtn];
//    
//    UILabel *lable = [[UILabel alloc] initWithFrame:CGRectMake(WIDTH / 4, 0, WIDTH / 2, 40)];
//    //    lable.backgroundColor = [UIColor redColor];
//    lable.text = @"品牌详情";
//    lable.font = [UIFont systemFontOfSize:20];
//    lable.textAlignment = NSTextAlignmentCenter;
//    [self.backView addSubview:lable];
//    
//    
//}
-(void)backBtnAction{
    [self.navigationController popViewControllerAnimated:YES];
}
// 第二部分订单状态
-(void)addOrderStatesUI{
    UIView *statesView = [[UIView alloc] initWithFrame:CGRectMake(0, 60 + HEIGHT/9, WIDTH, HEIGHT/13)];
    statesView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:statesView];
    
    for (NSInteger i = 0; i < 4; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(WIDTH/4*i, 0, WIDTH/4, 40);
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor whiteColor];
        [btn setTitle:buttonArr[i] forState:UIControlStateNormal];
        btn.tag = 2000 + i;
        [btn setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [statesView addSubview:btn];
    }
}
-(void)btnAction:(UIButton *)button {
    NSLog(@"%ld",button.tag);
    btnTag = button.tag;
    [self setBtnColor];
    [button setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    //    button.selected = !button.selected;
}
- (void)setBtnColor
{
    for (int i = 2000; i < 2004; i++) {
        UIButton *btn = (UIButton*)[self.view viewWithTag:i];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
    }
}

- (void)addOtherUI {
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 60, WIDTH, HEIGHT/9)];
    imgView.backgroundColor = [UIColor grayColor];
    imgView.image = [UIImage imageNamed:@"top"];
    [self.view addSubview:imgView];
    
    leftbackView = [[UIView alloc] initWithFrame:CGRectMake(0, 60 + HEIGHT/9 + HEIGHT/13 + 2, (480*WIDTH)/1242, HEIGHT)];
    leftbackView.backgroundColor = [UIColor colorWithWhite:0.976 alpha:1.000];
    [self.view addSubview:leftbackView];
    
    
    NSArray *titleArr = @[@"麻将机", @"麻将/棋牌", @"茶叶/食品",@"配件/工具"];
    NSArray *imageArr = @[@"keshilvhui", @"leibielvhui", @"julilvhui",@"2342"];
    NSArray *imageSelectArr = @[@"keshilv", @"leibielv", @"julilv",@"2312"];
    
    
    for (NSInteger i=0; i<4; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, ((200*HEIGHT)/2208)*i, (480*WIDTH)/1242, (200*HEIGHT)/2208);
        [button setTitle:titleArr[i] forState:UIControlStateNormal];
        [button setImage:[UIImage imageNamed:imageArr[i]] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [leftbackView addSubview:button];
        [button setImage:[UIImage imageNamed:imageSelectArr[i]] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor colorWithRed:49/255. green:194/255. blue:124/255. alpha:1.000] forState:UIControlStateSelected];
        [button setTitleColor:[UIColor colorWithRed:56/255. green:56/255. blue:56/255. alpha:1.000] forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        button.tag = 900+i;
        if (i==0) {
            button.selected = YES;
            button.backgroundColor = [UIColor whiteColor];
        }
        [button addTarget:self action:@selector(buttonSelect:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    
    
}


- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    NSLog(@"123");
    return YES;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
//    [seachField resignFirstResponder];
}
- (void)buttonSelect:(UIButton *)sender {
    //    sender.selected = !sender.selected;
    
    if (sender.tag == 900) {
        
        
    }
    if (sender.tag == 901) {
        
        
    }
    if (sender.tag == 902) {
        
        
        
    }
    if (sender.tag == 903) {
        
        
        
    }
    for (UIButton *button in leftbackView.subviews) {
        if (sender.tag == button.tag) {
            button.selected = YES;
            button.backgroundColor = [UIColor whiteColor];
        }else {
            button.selected = NO;
            button.backgroundColor = [UIColor colorWithWhite:0.976 alpha:1.000];
        }
    }
    
    
}

- (void)addNearlySelectTableviewUI {
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    //设置item大小
    flowLayout.itemSize = CGSizeMake((WIDTH-(480*WIDTH)/1242)/2-10, (WIDTH-(480*WIDTH)/1242)/2-10);
    //设置item与屏幕四周的间距
    //边界距离:上,左,下,右
    flowLayout.sectionInset = UIEdgeInsetsMake(5, 5, 5, 5);
    //设置最小行间距
    flowLayout.minimumLineSpacing = 1;
    //设置最小列间距
    flowLayout.minimumInteritemSpacing = 1;
    
    
    self.myCollectionVIew = [[UICollectionView alloc] initWithFrame:CGRectMake((480*WIDTH)/1242, 60+HEIGHT/9+HEIGHT/13 + 2, WIDTH-(480*WIDTH)/1242, HEIGHT) collectionViewLayout:flowLayout];
    [self.view addSubview:self.myCollectionVIew];
    self.myCollectionVIew.delegate = self;
    self.myCollectionVIew.dataSource = self;
    self.myCollectionVIew.backgroundColor = [UIColor clearColor];
    [self.myCollectionVIew registerNib:[UINib nibWithNibName:@"ClassifyCollectionViewCell" bundle:nil] forCellWithReuseIdentifier:@"cell"];
    
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return 4;
}
-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ClassifyCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    return cell;
}
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
    self.navigationController.navigationBar.translucent = YES;
}
-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    self.navigationController.navigationBar.translucent = NO;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
